package com.opengw.controller.processor;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.opengw.configuration.ConfigurationManager;
import com.opengw.configuration.route.Route;
import com.opengw.configuration.support.WSDLInitializer;
import com.opengw.exception.OpenGWException;
import com.opengw.http.HTTPConnection;
import com.opengw.http.HTTPConstants;
import com.opengw.http.HTTPRequest;
import com.opengw.http.HTTPResponse;

public class RouteProcessor extends BaseProcessor{
	
	protected RouteProcessor(){
		
	}

	@Override
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		try{
			ConfigurationManager configurationManager = ConfigurationManager.getConfigurationManager();
			Route route = configurationManager.getRoute(request.getRequestURI()); 
			
			WSDLInitializer wsdlCreator = new WSDLInitializer("http://gw.esb.turkiye.gov.tr:8282/ssg/wsdl?serviceoid=15499293");
			wsdlCreator.setEndPointURL("endpointAdres");
			wsdlCreator.changeXSDImport("http://ws.kullanici.turkiye.gov.tr/", "KullaniciXSD");
			wsdlCreator.changeXSDImport("http://oturum.turkiye.gov.tr/", "oturumXSD");
			System.out.println(new String(wsdlCreator.toByteArray()));
			
			if(route == null)
				throw new OpenGWException(OpenGWException.NOT_FOUND_RESOURCE, true);
			
			if(!route.getAvailableMethods().contains(request.getMethod().toUpperCase()))
				throw new OpenGWException(OpenGWException.FORBIDDEN_HTTP_METHOD, true); 
			
			
			HTTPConnection connection = null;
			
			System.out.println(route.getRouteURL());
			if(route.isConfigBeforeRequest()){
				HTTPRequest httpRequest = new HTTPRequest(request, route);
				connection = new HTTPConnection(httpRequest);
				connection.connect();
			} else{
				connection = new HTTPConnection(route.getRouteURL());
				connection.setRequestHeaders(request);
				if(request.getMethod().toUpperCase().contains(HTTPConstants.HTTP_METHOD_POST))
					connection.setDoOutput(true);
				connection.setDoInput(true);
				connection.connect();
				if(request.getContentLength() != -1)
					connection.transferFromStream(request.getInputStream(), request.getContentLength());
			}
			
			if(route.isConfigAfterResponse()){
				HTTPResponse httpResponse = new HTTPResponse(connection, route);
				httpResponse.sendResponse(response);
			} else{
				connection.sendResponse(response);
			}
			
		} catch (OpenGWException e) {
			if(e.isHTTPResponseAvaiable()){
				e.getResponse().sendResponse(response);
			} else{
				RequestDispatcher dispatcher = request.getRequestDispatcher(e.getExceptionPage());
				dispatcher.forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher dispatcher = request.getRequestDispatcher(OpenGWException.DEFAULT_ERROR_PAGE);
			dispatcher.forward(request, response);
		}
		
	}

}
